package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.RoomCount;
import com.example.demo.service.IBookService;
import com.example.demo.service.IRoomCountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BookController {

    @Autowired
    private IBookService bookService;

    @Autowired
    private IRoomCountService roomCountService;

    @RequestMapping("/bookList")
    public String list(Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Book> bookList = bookService.selectAll();
        PageInfo pageInfo=new PageInfo(bookList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("bookList", bookList);
        return "admin/book/bookList";
    }

    @RequestMapping("/toEditBook")
    public String toedit(Model model, Integer id) {

        if (id != null) {
            Book book = bookService.selectById(id);
            model.addAttribute("book", book);
            List<RoomCount> roomcount = roomCountService.selectByType(book.getType());
            model.addAttribute("roomcount", roomcount);
        }

        return "admin/book/edit";
    }

    @RequestMapping("/bookDatail")
    public String bookDatail(Model model, Integer id) {

        if (id != null) {
            Book book = bookService.selectById(id);
            model.addAttribute("book", book);

        }

        return "admin/book/editDetail";
    }

    @RequestMapping("/editBook")
    public String edit(Book book,Integer roomId) {

        if (book.getId() != null ) {
            bookService.update(book);
            roomCountService.updateStatus(roomId);
        } else {
            bookService.insert(book);
        }

        return "redirect:bookList";
    }

    @RequestMapping("/deleteBook")
    public String delete(Integer id) {

        bookService.delete(id);

        return "redirect:bookList";
    }

    @RequestMapping("/tuifang")
    public String tuifang(Integer id) {

        if (id != null ) {
            System.out.println("退房");
            Book book = bookService.selectById(id);
            Integer roomId = book.getRoomId();
            roomCountService.updateStatusToZero(roomId);
            bookService.delete(id);
        }

        return "redirect:bookList";
    }

    @RequestMapping("/selectBookByKey" )
    public String selectBookByKey(Model model, HttpServletRequest request,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum) {
        String tel = request.getParameter("tel");
        PageHelper.startPage(pageNum, 5);
        List<Book> bookList = bookService.selectByKey(tel);
        PageInfo pageInfo=new PageInfo(bookList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("bookList", bookList);
        return "admin/book/bookList";
    }

}
